import ky from './ky';
console.log(window.GlobalFetchOptions);

const options = Object.assign({
    prefixUrl: '',
    retry: 0,
    method: 'post',
    sysName: 'ajkh_',
    hooks: {
        beforeRequest: [],
        afterResponse: []
    }
}, window.GlobalFetchOptions || {});

options.hooks.beforeRequest.push((request) => {
    request.headers.set('X-Requested-With', 'XMLHttpRequest');
    request.headers.set('Content-Type', 'applocation/json');
});
options.hooks.afterResponse.push((request, options, response) => {
    // 未登录
    if (response.status === 401) {
        options.gotoLogin && options.gotoLogin();
        return;
    }
    // 过期
    if (response.status === 403) {
        localStorage.removeItem(`${options.sysName}auth_token`);
        options.gotoLogin && options.gotoLogin();
        return;
    }

    if (response.status === 404) {
        console.log('接口不存在');
        return;
    }
    return response;
});


const api = ky.extend(options);

export default api;
